package com.example.im;

import androidx.appcompat.app.AlertDialog;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.adapter.FriendListAdapter;
import com.example.db.CommonData;
import com.example.model.Friends;
import com.example.util.BaseActivity;
import com.example.util.MyUtil;
import com.example.websocket.MyWebSocketClient;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.example.db.CommonData.commonData;
import static com.example.db.CommonData.getDbFriendList;

public class MainActivity extends BaseActivity {
    private TextView name;
    private ListView friend_list;
    private ImageButton my_head;
    private ImageButton add_btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定控件
        name = findViewById(R.id.name);
        add_btn = findViewById(R.id.add_btn);
        my_head = findViewById(R.id.my_head);
        friend_list = findViewById(R.id.friend_list);


        //绑定list View适配器
        bindAdapter();


        initAddFriendButton(add_btn);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //判断登录状态
        if (commonData.getUser() != null) {
            name.setText(commonData.getUser().getName());
            ((FriendListAdapter) friend_list.getAdapter()).flash();
        } else {
            name.setText("去登录");
            my_head.setImageResource(R.drawable.ic_avatar_no);
            add_btn.setEnabled(false);
        }
    }



    private void initAddFriendButton(ImageButton addBtn) {
        addBtn.setOnClickListener(e -> {
            Intent intent = new Intent(MainActivity.this, AddFriendsActivity.class);
            startActivity(intent);
        });
    }

    //绑定适配器
    private void bindAdapter() {
        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
        //数据包括：friend 好友姓名 消息已读状态 最后一条消息
        List<Friends> friendList = new ArrayList<>();

        List<Friends> dbFriendList = commonData.getUser() == null ? new ArrayList<>() : getDbFriendList(commonData.getUser().getName());

        friend_list.setAdapter(new FriendListAdapter(this, dbFriendList));
    }



    //切换登录账号/退出
    public void changeUser(View view) {
        if (commonData.getUser() != null) {
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("是否切换账号or退出登录")
                    .setIcon(R.drawable.ic_launcher)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //退出接口 当前账号切换为下线状态
                            CommonData.logout(MainActivity.this);
                        }
                    })

                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                        }
                    })
                    .create();
            alertDialog.show();
        } else {
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            //本Activity结束，退出该Activity
            this.finish();
            startActivity(intent);
        }
    }



}